package lainey.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lainey.ui.R;

public class ListDialog extends Dialog {

    private Context mContext;
    private TextView mTvTitle;

    private List<String> mData;
    private ListAdapter mAdapter;
    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener l) {
        mListener = l;
    }

    public interface OnItemClickListener {

        void OnItemClick(int position);

    }

    public ListDialog(@NonNull Context context) {
        super(context, R.style.UI_DefaultDialogStyle);
        initDialog(context);
    }

    private void initDialog(Context context) {
        mContext = context;

        setContentView(R.layout.ui_dialog_list);
        Window window = getWindow();
        if (window != null) {
            window.getAttributes().gravity = Gravity.CENTER;
        }
        setCancelable(false);
        setCanceledOnTouchOutside(true);
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            setOwnerActivity(activity);
        }

        mTvTitle = findViewById(R.id.tv_title);
        ListView listView = findViewById(R.id.list_view);

        mData = new ArrayList<>();
        mAdapter = new ListAdapter();
        listView.setAdapter(mAdapter);

        listView.setOnItemClickListener(
                (parent, view, position, id) -> {
                    if (mListener != null) {
                        mListener.OnItemClick(position);
                    }
                    dismiss();
                }
        );
    }

    public void setTitleText(String text) {
        mTvTitle.setText(text);
    }

    public void setTitleText(int resId) {
        mTvTitle.setText(resId);
    }

    public void setData(String[] data) {
        List<String> list = Arrays.asList(data);
        setData(list);
    }

    public void setData(List<String> list) {
        mData.clear();
        if (list != null) {
            mData.addAll(list);
        }
        mAdapter.notifyDataSetChanged();
    }

    private class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(mContext, R.layout.ui_item_lv, null);
                holder.name = convertView.findViewById(R.id.tv_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String item = mData.get(position);
            holder.name.setText(item);
            return convertView;
        }

    }

    private static class ViewHolder {

        TextView name;

    }


}
